package shake.log;

import org.apache.log4j.MDC;

import shake.annotation.Component;
import shake.annotation.Observer;
import shake.annotation.Startup;
import shake.annotation.Unwrap;
import shake.event.ShakeEvent;

import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;

@RequestScoped
@Startup
@Component
public class RequestIdManager {

	String reqeustId;

	@Inject
	public RequestIdManager(IdGenerator generator) {
		this.reqeustId = generator.next();
		MDC.put("requestId", reqeustId);
	}

	@Observer(value = ShakeEvent.DESTROY.class)
	public void destory(RequestIdManager idmanager) {
		MDC.remove("requestId");
	}

	@Unwrap(name = "requestId")
	public String getRequestId() {
		return reqeustId;
	}

}
